package imy.virtual.touch.service;


import android.os.SystemClock;
import android.view.MotionEvent;


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

import imy.virtual.touch.TouchCore;
import imy.virtual.touch.bean.GesturePoint;
import imy.virtual.touch.constant.MotionEventExt;
import imy.virtual.touch.helper.MotionEventHelper;
import imy.virtual.touch.interfaces.GestureInterface;

public final class GestureManagerService implements GestureInterface {
    private static final String TAG = "GestureManagerService";
    private static GestureManagerService gCore = new GestureManagerService();
    private static final long REGULAR_CLICK_LENGTH = 100;
    private ExecutorService executorService;

    public GestureManagerService() {
        executorService = Executors.newSingleThreadExecutor();
    }

    public static GestureManagerService get() {
        return gCore;
    }

    @Override
    public void onDown(GesturePoint point) {
        synchronized (this) {

            MotionEvent event = MotionEventHelper.get().createEvent(point);
            EventSendThread obtain = EventSendThread.obtain(event, point.id);
            executorService.execute(obtain);
        }
    }

    @Override
    public synchronized void onUp(GesturePoint point) {
        synchronized (this) {

            MotionEvent event = MotionEventHelper.get().releasedMotionEvent(point);
            EventSendThread obtain = EventSendThread.obtain(event, point.id);
            executorService.execute(obtain);
        }
    }

    @Override
    public void onMove(GesturePoint point) {
        synchronized (this) {

        }
    }

    @Override
    public void onClicked(GesturePoint point) {
        //取得Event
        if (point.action== MotionEventExt.MOTION_CLICKED){
            point.action=MotionEvent.ACTION_DOWN;
        }
        onDown(point);
        SystemClock.sleep(REGULAR_CLICK_LENGTH);
        point.action=MotionEvent.ACTION_UP;
        onUp(point);
    }


}
